package com.example.test02.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.test02.R;

public class NewsAdapter extends BaseAdapter {
    private Context context;
    private String[] titles;
    private String[] descriptions;
    private String[] dates;

    public NewsAdapter(Context context, String[] titles, String[] descriptions, String[] dates) {
        this.context = context;
        this.titles = titles;
        this.descriptions = descriptions;
        this.dates = dates;
    }

    @Override
    public int getCount() {
        return titles.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.news_list, parent, false);
        }

        ImageView icon = convertView.findViewById(R.id.item_icon);
        TextView title = convertView.findViewById(R.id.item_title);
        TextView description = convertView.findViewById(R.id.item_description);
        TextView date = convertView.findViewById(R.id.item_date);
        TextView time = convertView.findViewById(R.id.item_time); // 确保 item_time 已经在布局文件中定义

        // 根据 description 的内容设置图标
        if (descriptions[position].contains("未")) {
            icon.setImageResource(R.drawable.not_fake_safe);
        } else {
            icon.setImageResource(R.drawable.warnning);
        }

        title.setText(titles[position]);
        description.setText(descriptions[position]);

        // 处理日期和时间
        String dateTime = dates[position];
        if (dateTime.contains(" ")) {
            String[] parts = dateTime.split(" "); // 按照空格分割日期和时间
            if (parts.length > 1) {
                date.setText(parts[0]); // 设置日期部分
                time.setText(parts[1].substring(0, 5)); // 设置时间部分（HH:mm）
            } else {
                date.setText("未知日期");
                time.setText("未知时间");
            }
        } else {
            date.setText("未知日期");
            time.setText("未知时间");
        }

        return convertView;
    }


}
